
#include <disk_matrix.h>
#include <gtest/gtest.h>
	
	
int main(int argc, char **argv)
{
 	testing::InitGoogleTest(&argc, argv);
	return RUN_ALL_TESTS();
}

/**
 * @brief 
**/
class test_disk_matrix_t_disk_matrix_t_suite : public ::testing::Test
{
protected:
	test_disk_matrix_t_disk_matrix_t_suite(){};
	virtual ~test_disk_matrix_t_disk_matrix_t_suite(){};
	virtual void SetUp()
	{
		//Called befor every TEST_F(test_disk_matrix_t_disk_matrix_t_suite, *)
	};
	virtual void TearDown()
	{
		//Called after every TEST_F(test_disk_matrix_t_disk_matrix_t_suite, *)
	};
};
 
/**
 * @brief 
 * @begin_version 
**/
TEST_F(test_disk_matrix_t_disk_matrix_t_suite, case_name1)
{
}
 
/**
 * @brief 
**/
class test_create_suite : public ::testing::Test{
    protected:
        test_create_suite(){};
        virtual ~test_create_suite(){};
        virtual void SetUp() {
            //Called befor every TEST_F(test_create_suite, *)
        };
        virtual void TearDown() {
            //Called after every TEST_F(test_create_suite, *)
        };
};
 
/**
 * @brief 
 * @begin_version 
**/
TEST_F(test_create_suite, case_name1)
{
	//TODO
}
 
/**
 * @brief 
**/
class test_disk_matrix_t_save_suite : public ::testing::Test
{
protected:
	test_disk_matrix_t_save_suite(){};
	virtual ~test_disk_matrix_t_save_suite(){};
	virtual void SetUp()
	{
		//Called befor every TEST_F(test_disk_matrix_t_save_suite, *)
	};
	virtual void TearDown()
	{
		//Called after every TEST_F(test_disk_matrix_t_save_suite, *)
	};

};
 
/**
 * @brief 
 * @begin_version 
**/
TEST_F(test_disk_matrix_t_save_suite, save_large)
{
	std::vector<pitem> items;
	items.reserve(5000000);
	for(int i = 0; i < 500000; i++)
	{
		pitem p(new item_t);
		p->row = 1;
		p->col = i + 1;
		p->value = i + 2;
		items.push_back(p);
	}

	pcache cache;
	vector_iterator_t vec_ite(items);
	disk_matrix_t map1("./file_map.sst", cache, bytes_t::uint64_comparator,
			bytes_t::uint64_comparator);
	int ret = map1.save(vec_ite);
	EXPECT_EQ(0, ret);
}

/**
 * @brief
**/
class test_disk_matrix_t_load_suite : public ::testing::Test
{
protected:
	test_disk_matrix_t_load_suite(){};
	virtual ~test_disk_matrix_t_load_suite(){};
	virtual void SetUp()
	{
	};
	virtual void TearDown()
	{
		//Called after every TEST_F(test_disk_matrix_t_load_suite, *)
	};

};

/**
 * @brief
 * @begin_version
**/
TEST_F(test_disk_matrix_t_load_suite, load_large)
{
	// create a sst which contains a lots of items.
	std::vector<pitem> items;
	items.reserve(5000000);
	for(int i = 0; i < 500000; i++)
	{
		pitem p(new item_t);
		p->row = 1;
		p->col = i + 1;
		p->value = i + 2;
		items.push_back(p);
	}
	pcache cache;
	vector_iterator_t vec_ite(items);
	disk_matrix_t map1("./disk_matrix.sst", cache);
	map1.save(vec_ite);

	// start to load.
	disk_matrix_t map("./disk_matrix.sst", cache);
	int ret = map.load();
	EXPECT_EQ(0, ret);

	int i = 0;
	disk_matrix_iterator_t ite(map);
	ite.reset();
	pitem curr_item;
	while(ite.get_next(curr_item) == 0)
	{
		uint64_t index_key = (uint64_t)(curr_item->row);
		uint64_t extra_key = (uint64_t)(curr_item->col);
		uint64_t value = (uint64_t)(curr_item->value);
		EXPECT_EQ(1, index_key);
		EXPECT_EQ(i + 1, extra_key);
		EXPECT_EQ(i + 2, value);
		i++;
	}
}

/**
 * @brief
**/
class test_disk_matrix_t_find_suite : public ::testing::Test
{
protected:
test_disk_matrix_t_find_suite(){};
	virtual ~test_disk_matrix_t_find_suite(){};
	virtual void SetUp()
	{
	};
	virtual void TearDown()
	{
		//Called after every TEST_F(test_disk_matrix_t_save_suite, *)
	};

};

/**
 * @brief
 * @begin_version
**/
TEST_F(test_disk_matrix_t_find_suite, find_large)
{
	// create a sst which contains a lots of items.
	std::vector<pitem> items;
	items.reserve(5000000);
	for(int i = 0; i < 500000; i++)
	{
		pitem p(new item_t);
		p->row = 1;
		p->col = i + 1;
		p->value = i + 2;
		items.push_back(p);
	}
	pcache cache;
	vector_iterator_t vec_ite(items);
	disk_matrix_t map1("./disk_matrix.sst", cache);
	map1.save(vec_ite);

	// start to find.
	disk_matrix_t map("./disk_matrix.sst", cache);
	map.load();
	std::vector<pitem> res;
	bytes_t bytes(1);
	int ret = map.find(bytes, res);
	EXPECT_EQ(0, ret);
	cout << "matches: " << res.size() << endl;
}
 
